{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-27T09:37:10.830208Z",
     "start_time": "2024-07-27T09:37:10.814038Z"
    }
   },
   "source": [
    "import arcpy\n",
    "import numpy as np\n",
    "import os\n",
    "from glob import glob\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-27T09:52:08.609998Z",
     "start_time": "2024-07-27T09:47:12.883296Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data_folder=r\"G:\\PAPER3\\merge_data\"\n",
    "output_folder=r\"G:\\PAPER3\\merge_data_csv\"\n",
    "\n",
    "index = ['GOSIF', 'dem', 'aspect', 'EVI', 'LAI', 'LST_Day', 'LST_Night', 'NDVI', 'Nir', 'Red', 'slope']\n",
    "\n",
    "for file in glob(os.path.join(data_folder,\"*.tif\")):\n",
    "    \n",
    "    # 读取多波段影像\n",
    "    raster = arcpy.Raster(file)\n",
    "    \n",
    "    # 获取影像的波段数\n",
    "    band_count = raster.bandCount\n",
    "    \n",
    "    # 获取 NoData 值（假设所有波段有相同的 NoData 值）\n",
    "    nodata_value = raster.noDataValue\n",
    "    \n",
    "    # 创建一个空列表来存储所有波段的数据\n",
    "    all_band_data = []\n",
    "    \n",
    "    # 遍历每个波段\n",
    "    for band_index in range(1, band_count + 1):\n",
    "        # 获取当前波段的数据\n",
    "        band = arcpy.Raster(file + f\"/Band_{band_index}\")\n",
    "        band_array = arcpy.RasterToNumPyArray(band)\n",
    "        \n",
    "        # 将数据展平并添加到列表中\n",
    "        all_band_data.append(band_array.flatten())\n",
    "    \n",
    "    # 将所有波段的数据合并为一个二维数组，每列为一个波段的数据\n",
    "    all_band_data = np.array(all_band_data).T\n",
    "    \n",
    "    # 筛选出所有波段值都不为 NoData 的区域\n",
    "    valid_data_mask = np.all(all_band_data != nodata_value, axis=1)\n",
    "    valid_band_data = all_band_data[valid_data_mask]\n",
    "    \n",
    "    # 创建一个 DataFrame 来存储数据\n",
    "    df = pd.DataFrame(valid_band_data, columns=[index[i] for i in range(band_count)])\n",
    "    \n",
    "    # 删除包含-32768的行\n",
    "    df = df[df.applymap(lambda x: x != -32768).all(axis=1)]\n",
    "    \n",
    "    # 删除包含 NaN 值的行\n",
    "    df.dropna(axis=0, how='any', inplace=True)\n",
    "    \n",
    "    # 导出为 Excel 文件\n",
    "    output_excel = os.path.join(output_folder, os.path.basename(file).replace(\".tif\", \".xlsx\"))\n",
    "    df.to_excel(output_excel, index=False)\n",
    "    \n",
    "\n",
    "    "
   ],
   "id": "b841313f15a8683",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "ad18548d461157e6"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
